import  { ethers } from "hardhat";
// import {ethers} from "ethers";
  import {getMock}  from './VRFCoordinatorV2Mock'
export async function getConsumer() {
    //获得mock合约
    let mock=await getMock();
    let mockContract=mock.VRFCoordinatorV2MockContract;
    // 切换到默认签名者
    let siners=await ethers.getSigners();//获取签名者
    let deployer=siners[0];
    mockContract.connect(deployer) //mock合约连接到账号

  // 创建订阅
    let tx= await mockContract.createSubscription();
    let receipt=await tx.wait(1);
    let subscribeId=receipt.logs[0].topics[1]//获得订阅号
    console.log('订阅号为:'+subscribeId);
    // 充值
    await mockContract.fundSubscription(subscribeId,BigInt(100000000000000000000))
    // console.log(await mockContract.getSubscription(subscribeId))
    let vrfCoordinator=mockContract.getAddress();//获得mock合约地址
    // 创建消费者
    let keyHash="0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc";//号码任意
    const VRFConsumer = await ethers.getContractFactory("VRFConsumer",deployer);
    let VRFConsumerContract=await VRFConsumer.deploy(subscribeId,vrfCoordinator,keyHash);
    // 将subId授权给消费者合约
    let VRFConsumerAddress=await VRFConsumerContract.getAddress();
    mockContract.addConsumer(subscribeId,VRFConsumerAddress);
    console.log('Consumer is Deployed in=>'+VRFConsumerAddress +'    by=>'+deployer.address);
    return {mockContract,VRFConsumerContract}
}
// getConsumer()